package com.china08.yxyapi.api.manage.tec.teaching.live.handleLive;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.api.manage.tec.teaching.live.myClassSchedule.MyClassScheduleController;
import com.china08.yxyapi.entity.db.live.YxyLiveCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.live.YxyLiveCatalogRepository;

import io.swagger.annotations.ApiOperation;

/**
 * 我的教学-直播课-教师-直播查询
 * @author 赵亚辉
 *
 */
@CrossOrigin
@RestController
public class LiveDetailController extends YxyApiBaseController{
	
	private static final Logger logger=Logger.getLogger(MyClassScheduleController.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private YxyLiveCatalogRepository yxyLiveCatalogRepository;
	
	private static SimpleDateFormat sf1=new SimpleDateFormat("yyyy年MM月dd日");
	
	private static SimpleDateFormat sf2=new SimpleDateFormat("HH:mm");
	
	@ApiOperation(value = "直播课查询")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/live/liveCon/findOne", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel findOne(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
		
		RespModel respModel=new RespModel();
		
		String id = reqModel.id;
		
		String lessonDataSQL="select id,name,create_date,pv,ep_times,ep,cover,catalog_id1,catalog_id2,catalog_id3,scope,live_type,count,summary,"
				+ " audit_status,reject_reason,reject_date,up_date,is_visible,down_type,down_reason,down_date,status,catalog_school_id "
				+ " from yxy_live.yxy_live_lesson"
				+ " where id='"+id+"'";
		Map<String, Object> lesson = jdbcTemplate.queryForMap(lessonDataSQL);
		if(lesson.size()>0){
			Date createDate=(Date) lesson.get("create_date");
			if(createDate!=null){
				String createDateFormat=sf1.format(createDate)+" "+sf2.format(createDate);
				lesson.put("createDateFormat", createDateFormat);
			}
			Date rejectDate=(Date) lesson.get("reject_date");
			if(rejectDate!=null){
				String rejectDateFormat=sf1.format(rejectDate);
				lesson.put("rejectDateFormat", rejectDateFormat);
			}
			Date upDate=(Date) lesson.get("up_date");
			if(upDate!=null){
				String upDateFormat=sf1.format(upDate);
				lesson.put("upDateFormat", upDateFormat);
			}
			Date downDate=(Date) lesson.get("down_date");
			if(downDate!=null){
				String downDateFormat=sf1.format(downDate);
				lesson.put("downDateFormat", downDateFormat);
			}
			
			String catalogId1=(String) lesson.get("catalog_id1");
			if(StringUtils.isNotEmpty(catalogId1)){
				YxyLiveCatalog yxyLiveCatalog=yxyLiveCatalogRepository.findById(catalogId1);
				if(yxyLiveCatalog!=null){
					lesson.put("catalogName1", yxyLiveCatalog.getName());
				}
			}
			String catalogId2=(String) lesson.get("catalog_id2");
			if(StringUtils.isNotEmpty(catalogId2)){
				YxyLiveCatalog yxyLiveCatalog=yxyLiveCatalogRepository.findById(catalogId2);
				if(yxyLiveCatalog!=null){
					lesson.put("catalogName2", yxyLiveCatalog.getName());
				}
			}
			String catalogId3=(String) lesson.get("catalog_id3");
			if(StringUtils.isNotEmpty(catalogId3)){
				YxyLiveCatalog yxyLiveCatalog=yxyLiveCatalogRepository.findById(catalogId3);
				if(yxyLiveCatalog!=null){
					lesson.put("catalogName3", yxyLiveCatalog.getName());
				}
			}
			
			// auditStatus 0-待审核,1-审核通过,2-审核拒绝
			// isVisible 0-不显示(下架),1-可显示(上架)
			// status 课程状态0-未开始,1-直播中,2-已结束
			Integer auditStatus=(Integer) lesson.get("audit_status");
			Integer isVisible=(Integer) lesson.get("is_visible");
			Integer status=(Integer) lesson.get("status");
			if(auditStatus!=null&&isVisible!=null&&status!=null){
				// 自定义课程状态 方便判断
				// classStatus 0：待审核;1:未通过；2，已上架；3，已下架；4，已结束
				Integer classStatus=null;
				if(auditStatus==0){
					classStatus=0;
				}else if(auditStatus==2){
					classStatus=1;
				}else if(auditStatus==1){
					if(isVisible==0){
						classStatus=3;
					}else if(isVisible==1){
						if(status==2){
							classStatus=4;
						}else{
							classStatus=2;
						}
					}
				}
				if(classStatus!=null){
					lesson.put("classStatus", classStatus);
				}
			}
			
			respModel.lesson=lesson;
			
			String lessonItemDataSQL="select id lessonItemId,title,begin_date,end_date,status,record_open,duration from yxy_live.yxy_live_lesson_item"
					+ " where lesson_id='"+id+"' "
					+ " order by begin_date";
			List<Map<String, Object>> lessonItemList = jdbcTemplate.queryForList(lessonItemDataSQL);
			for(Map<String, Object> map:lessonItemList){
				Date beginDate=(Date) map.get("begin_date");
				Date endDate=(Date) map.get("end_date");
				if(beginDate!=null&&endDate!=null){
					String formatDate=sf1.format(beginDate)+" "+sf2.format(beginDate)+"-"+sf2.format(endDate);
					map.put("formatDate", formatDate);
					
					String dateLessonItem=sf1.format(beginDate);
					map.put("dateLessonItem", dateLessonItem);
					String timeStartLessonItem=sf2.format(beginDate);
					map.put("timeStartLessonItem", timeStartLessonItem);
					String timeEndLessonItem=sf2.format(beginDate);
					map.put("timeEndLessonItem", timeEndLessonItem);
				}
			}
			
			respModel.lessonItemList=lessonItemList;
		}
		
		return respModel;
	}
	
	protected static class ReqModel extends YxyApiReqModel{
		
		public String id;
		
		@Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "请填写课程id");
        }
	}
	
	protected static class RespModel {
		
		public Map<String, Object> lesson;
		
		public List<Map<String, Object>> lessonItemList;

	}

}
